function varargout = chapter9_cyclonedesign(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @chapter9_cyclonedesign_OpeningFcn, ...
                   'gui_OutputFcn',  @chapter9_cyclonedesign_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


function chapter9_cyclonedesign_OpeningFcn(hObject, eventdata, handles, varargin)
%include functions
addpath(genpath('chapter9'));
addpath(genpath('shared'));
addpath(genpath('conversion'));
handles.vis=0;
handles.denP=0;
handles.den=0;
handles.si=0;
handles.volF=0;
handles.pressureDrop=0;
handles.Eu=0;
handles.Stk50=0;
handles.strBedHeight='m';
handles.strQ='m3/s';
handles.strUfsv='m/s';
handles.strDenP='kg/m3';
handles.strVis='Pas';
handles.strSi='m';
handles.strDen='kg/m3';
handles.strPressure='Pa';
handles.VIS=-1;
handles.DENP=-1;
handles.DEN=-1;
handles.SI=-1;
handles.VOLF=-1;
handles.PRESSURE=-1;
handles.EU=-1;
handles.STK50=-1;
set(handles.HE,'Value',0);
set(handles.HR,'Value',0);
set(handles.others,'Value',1);
set(handles.stairmandEu,'Visible','Off');
set(handles.stairmandStokes,'Visible','Off');
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


function varargout = chapter9_cyclonedesign_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;



function size_Callback(hObject, eventdata, handles)
si= str2double(get(hObject,'String'));
handles.si = si;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function size_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function density_Callback(hObject, eventdata, handles)
den= str2double(get(hObject,'String'));
handles.den = den;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function density_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function denP_Callback(hObject, eventdata, handles)
denP= str2double(get(hObject,'String'));
handles.denP = denP;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function denP_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function viscosity_Callback(hObject, eventdata, handles)
vis= str2double(get(hObject,'String'));
handles.vis = vis;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function viscosity_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in menuSi.
function menuSi_Callback(hObject, eventdata, handles)
contents = get(hObject,'Value');
switch contents
    case 1
        strSi='m';
    case 2
        strSi='cm';
    case 3
        strSi='mm';
    case 4
        strSi='um';
    case 5
        strSi='ft';
    case 6
        strSi='in';
end
handles.strSi = strSi;
guidata(hObject,handles); 


% --- Executes during object creation, after setting all properties.
function menuSi_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in menuDen.
function menuDen_Callback(hObject, eventdata, handles)
contents = get(hObject,'Value');
switch contents
    case {1,2}
        strDen='kg/m3';
    case {3}
        strDen='lb/ft3';
    case {4,5}
        strDen='g/cm3';
end
handles.strDen = strDen;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function menuDen_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in menudenP.
function menudenP_Callback(hObject, eventdata, handles)
contents = get(hObject,'Value');
switch contents
    case {1,2}
        strDenP='kg/m3';
    case {3}
        strDenP='lb/ft3';
    case {4,5}
        strDenP='g/cm3';
end
handles.strDenP = strDenP;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function menudenP_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in menuVis.
function menuVis_Callback(hObject, eventdata, handles)
contents = get(hObject,'Value');
switch contents
    case 1
        strVis='Pas';
    case 2
        strVis='cp';
    case 3
        strVis='psf.s';
    case 4
        strVis='lb/ft/s';
end
handles.strVis = strVis;
guidata(hObject,handles); 

% --- Executes during object creation, after setting all properties.
function menuVis_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in calculate.
function calculate_Callback(hObject, eventdata, handles)
size=handles.SI;
denF= convertDensity(handles.den,handles.strDen);
denP= convertDensity(handles.denP,handles.strDenP);
cutsize= convertLength(handles.si,handles.strSi);
pressure=convertPressure(handles.pressureDrop,handles.strPressure);
vis=convertViscosity(handles.vis,handles.strVis);
Q=convertVolumetricFlowRate(handles.volF,handles.strQ);
Eu=handles.Eu;
Stk50=handles.Stk50;
errorCode=validateInput_chapter9_cyclonedesign(denF,denP,vis,cutsize,Eu,Stk50,pressure,Q);
if(~isempty(errorCode))
    errorMessage = getErrorMessage(errorCode);
    errorMessage = sprintf(errorMessage);
    set(handles.errorBox,'String',errorMessage);
    set(handles.numCyclones,'String','-');
    set(handles.cycloneDiameter,'String','-');
    set(handles.actualCutSize,'String','-');
    handles.SI=-2;
    guidata(hObject,handles); 
else
    set(handles.errorBox,'String','');
    [cycloneDiameter numCyclones actualCutSize]=findCycloneDesign(cutsize,Q,denP,denF,vis,pressure,Eu,Stk50);
    set(handles.numCyclones,'String',sprintf('%d',numCyclones));
    set(handles.cycloneDiameter,'String',sprintf('%.5f m',cycloneDiameter));
    set(handles.actualCutSize,'String',sprintf('%.3f um',actualCutSize*1000000));
    handles.VIS=vis;
    handles.DENP=denP;
    handles.DEN=denF;
    handles.SI=cutsize;
    handles.VOLF=Q;
    handles.PRESSURE=pressure;
    handles.EU=Eu;
    handles.STK50=Stk50;
    guidata(hObject,handles); 
end

function Stk50_Callback(hObject, eventdata, handles)
Ufsv = str2double(get(hObject,'String'));
handles.Stk50 = Ufsv;
guidata(hObject,handles); 

function Stk50_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function volF_Callback(hObject, eventdata, handles)
Mp = str2double(get(hObject,'String'));
handles.volF = Mp;
guidata(hObject,handles); 

function volF_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function pressureDrop_Callback(hObject, eventdata, handles)
dia= str2double(get(hObject,'String'));
handles.pressureDrop = dia;
guidata(hObject,handles); 


function pressureDrop_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function menuQ_Callback(hObject, eventdata, handles)
contents = get(hObject,'Value');
switch contents
    case 1
        tmpstr='m3/s';
    case 2
        tmpstr='m3/min';
    case 3
        tmpstr='m3/h';
    case 4
        tmpstr='cm3/s';
    case 5
        tmpstr='cm3/min';
    case 6
        tmpstr='cm3/h';
    case 7
        tmpstr='L/s';
    case 8
        tmpstr='L/min';
    case 9
        tmpstr='L/h';
    case 10
        tmpstr='ft3/s';
    case 11
        tmpstr='ft3/min';
    case 12
        tmpstr='ft3/h';
end
handles.strQ = tmpstr;
guidata(hObject,handles);

function menuQ_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function menuPressure_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function menuPressure_Callback(hObject, eventdata, handles)
contents = get(hObject,'Value');
switch contents
    case 1
        tmpstr='Pa';
    case 2
        tmpstr='kpa';
    case 3
        tmpstr='atm';
    case 4
        tmpstr='bar';
    case 5
        tmpstr='psf';
    case 6
        tmpstr='psi';
    case 7
        tmpstr='mmHg';
    case 8
        tmpstr='mmh2o';
    case 9
        tmpstr='lb/(fts2)';
end
handles.strPressure = tmpstr;
guidata(hObject,handles);


function Eu_Callback(hObject, eventdata, handles)
tmp= str2double(get(hObject,'String'));
handles.Eu = tmp;
guidata(hObject,handles); 


function Eu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in showWorking.
function showWorking_Callback(hObject, eventdata, handles)
vis=handles.VIS;
denP=handles.DENP;
denF=handles.DEN;
size=handles.SI;
pressure=handles.PRESSURE;
Eu=handles.EU;
Stk50=handles.STK50;
Q=handles.VOLF;
if (size==-2)
    errorMessage=sprintf('ERROR!\n\nInvalid input\n\nPlease reenter values and calculate before showing working');
     set(handles.errorBox,'String',errorMessage);
elseif (size==-1)
    errorMessage=sprintf('ERROR!\n\nPlease calculate before showing working');
    set(handles.errorBox,'String',errorMessage);
else
    setappdata(0,'denF',denF);
    setappdata(0,'denP',denP);
    setappdata(0,'vis',vis);
    setappdata(0,'Q',Q);
    setappdata(0,'size',size);
    setappdata(0,'pressure',pressure);
    setappdata(0,'Eu',Eu);
    setappdata(0,'Stk50',Stk50);
    chapter9_cyclonedesign_calculation
end
    


% --- Executes on button press in HE.
function HE_Callback(hObject, eventdata, handles)
set(handles.HE,'Value',1);
set(handles.HR,'Value',0);
set(handles.others,'Value',0);
set(handles.stairmandEu,'Visible','On');
set(handles.stairmandStokes,'Visible','On');
set(handles.stairmandEu,'String','320');
set(handles.stairmandStokes,'String','0.00014');
handles.Eu=320;
handles.Stk50=0.00014;
guidata(hObject,handles); 

% --- Executes on button press in HR.
function HR_Callback(hObject, eventdata, handles)
set(handles.HR,'Value',1);
set(handles.HE,'Value',0);
set(handles.others,'Value',0);
set(handles.stairmandEu,'Visible','On');
set(handles.stairmandStokes,'Visible','On');
set(handles.stairmandEu,'String','46');
set(handles.stairmandStokes,'String','0.006');
handles.Eu=46;
handles.Stk50=0.006;
guidata(hObject,handles); 


% --- Executes on button press in others.
function others_Callback(hObject, eventdata, handles)
set(handles.others,'Value',1);
set(handles.HE,'Value',0);
set(handles.HR,'Value',0);
set(handles.stairmandEu,'Visible','Off');
set(handles.stairmandStokes,'Visible','Off');
guidata(hObject,handles); 
